package com.jeremyfeinstein.conference;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.LayoutRes;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.jeremyfeinstein.slidingmenu.lib.R;


/**
 * Created by sunxunchao on 2018/6/26.
 */
public class CustomDialog {

    private Dialog dialog;
    private View view;
    private @LayoutRes
    int layoutId;

    public CustomDialog() {
    }

    public CustomDialog builder(Context context) {
        dialog = new Dialog(context, R.style.common_dialog);
        view = LayoutInflater.from(context).inflate(layoutId, null);
        dialog.setContentView(view);
        return this;
    }
    public CustomDialog builder(Context context, @StyleRes int themeResId) {
        dialog = new Dialog(context, themeResId);
        view = LayoutInflater.from(context).inflate(layoutId, null);
        dialog.setContentView(view);
        return this;
    }

    public CustomDialog create() {
        if (dialog != null) {
            Window window = dialog.getWindow();
            window.setGravity(Gravity.CENTER);  //此处可以设置dialog显示的位置
            window.setWindowAnimations(R.style.popup_animation);  //添加动画
        }
        return this;
    }

    public CustomDialog viewManager(CallBack callBack) {
        if (dialog != null && view != null) {
            callBack.handleView(dialog, view);
        }
        return this;
    }

    public CustomDialog setView(@LayoutRes int layoutId) {
        this.layoutId = layoutId;
        return this;
    }

    public CustomDialog setCanceledOnTouchOutside(boolean cancel) {
        if (dialog == null) return this;
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public CustomDialog setCancelable(boolean flag) {
        if (dialog == null) return this;
        dialog.setCancelable(flag);
        return this;
    }

    public CustomDialog setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        if (dialog != null) {
            dialog.setOnDismissListener(onDismissListener);
        }
        return this;
    }

    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public boolean isShowing() {
        if (dialog != null) {
           return dialog.isShowing();
        }else {
            return false;
        }
    }


    public interface CallBack {
        void handleView(Dialog dialog, View dialogView);
    }
}
